package com.yanqu.road.dao.impl.monopolymarket;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.group.MonopolyMarketServerGroup;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/9/6 10:34
 */
public class MonopolyMarketServerGroupDaoImpl extends TempDataObjectDao<MonopolyMarketServerGroup> {
    public static String getMonopolyMarketServerGroupSql = "select * from `t_s_monopolymarket_server_group` where `serverId`=?";
    public MonopolyMarketServerGroup getMonopolyMarketServerGroup(long serverId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketServerGroupSql);
            int i = 1;
            call.setLong(i++, serverId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateMonopolyMarketServerGroupSql = "update `t_s_monopolymarket_server_group` set channelId=?,groupId=?,serverList=?,state=?,fortressState=?,changTime=?,officeCfgChangTime=?,kingCfgChangTime=?,queenCfgChangTime=?,fortressLockJoinUnionLastTime=?,fortressLockJoinUnionMemberLastTime=?,fortressCanJoinUnionIdList=? where serverId=?";
    @Override
    protected boolean updateData(MonopolyMarketServerGroup monopolyMarketServerGroup) {
        monopolyMarketServerGroup.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getChannelId()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketServerGroup.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketServerGroup.getServerList())));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getState()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getFortressState()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getOfficeCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getKingCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getQueenCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getFortressLockJoinUnionLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getFortressLockJoinUnionMemberLastTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketServerGroup.getFortressCanJoinUnionIdList())));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketServerGroup.getServerId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketServerGroupSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketServerGroupSql = "insert into `t_s_monopolymarket_server_group`(serverId,channelId,groupId,serverList,state,fortressState,changTime,officeCfgChangTime,kingCfgChangTime,queenCfgChangTime,fortressLockJoinUnionLastTime,fortressLockJoinUnionMemberLastTime,fortressCanJoinUnionIdList) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketServerGroup monopolyMarketServerGroup) {
        monopolyMarketServerGroup.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketServerGroup.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getChannelId()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketServerGroup.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketServerGroup.getServerList())));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getState()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getFortressState()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getOfficeCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getKingCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getQueenCfgChangTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getFortressLockJoinUnionLastTime()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketServerGroup.getFortressLockJoinUnionMemberLastTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(monopolyMarketServerGroup.getFortressCanJoinUnionIdList())));
        if (getDb().executeNoneQuery(insertMonopolyMarketServerGroupSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketServerGroup parseData(ResultSet rs) throws SQLException {
        MonopolyMarketServerGroup monopolyMarketServerGroup = new MonopolyMarketServerGroup();
        monopolyMarketServerGroup.setServerId(rs.getLong("serverId"));
        monopolyMarketServerGroup.setChannelId(rs.getInt("channelId"));
        monopolyMarketServerGroup.setGroupId(rs.getLong("groupId"));
        monopolyMarketServerGroup.setServerList(JSON.parseObject(rs.getString("serverList"), new TypeReference<java.util.List<java.lang.Long>>() {}));
        monopolyMarketServerGroup.setState(rs.getInt("state"));
        monopolyMarketServerGroup.setFortressState(rs.getInt("fortressState"));
        monopolyMarketServerGroup.setChangTime(rs.getInt("changTime"));
        monopolyMarketServerGroup.setOfficeCfgChangTime(rs.getInt("officeCfgChangTime"));
        monopolyMarketServerGroup.setKingCfgChangTime(rs.getInt("kingCfgChangTime"));
        monopolyMarketServerGroup.setQueenCfgChangTime(rs.getInt("queenCfgChangTime"));
        monopolyMarketServerGroup.setFortressLockJoinUnionLastTime(rs.getInt("fortressLockJoinUnionLastTime"));
        monopolyMarketServerGroup.setFortressLockJoinUnionMemberLastTime(rs.getInt("fortressLockJoinUnionMemberLastTime"));
        monopolyMarketServerGroup.setFortressCanJoinUnionIdList(JSON.parseObject(rs.getString("fortressCanJoinUnionIdList"), new TypeReference<java.util.List<java.lang.String>>() {}));

        return monopolyMarketServerGroup;
    }


}
